<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="EduBot - 智能 PPTX 教案生成工具，帮助教师快速生成高质量教案">
    <title>EduBot - PPTX 教案生成</title>
    <link rel="icon" href="/static/icons/AiKeTangJiaoXuePingCeXiTong.png" type="image/png">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.10.1/jszip.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
    <script src="/static/javascripts/PptxGeneratesLessonPlans.js"></script>
    <style>
        :root {
            --primary-color: #00ff9d;
            --secondary-color: #ff00ff;
            --background-color: #0a0a0a;
            --card-background: #1a1a1a;
            --text-color: #ffffff;
            --border-color: #333333;
            --hover-color: #00cc7d;
            --error-color: #ff3366;
            --success-color: #00ff9d;
        }

        body {
            font-family: 'Microsoft YaHei', sans-serif;
            margin: 0;
            padding: 15px;
            background: var(--background-color);
            color: var(--text-color);
            min-height: 100vh;
        }

        .header {
            display: grid;
            grid-template-columns: auto 1fr auto;
            align-items: center;
            padding: 15px 20px;
            background: var(--card-background);
            box-shadow: 0 0 20px rgba(0, 255, 157, 0.1);
            margin-bottom: 20px;
            border: 1px solid var(--border-color);
            border-radius: 8px;
            position: relative;
            overflow: hidden;
        }

        .header::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 2px;
            background: linear-gradient(90deg, var(--primary-color), var(--secondary-color));
        }

        .header-buttons {
            display: flex;
            align-items: center;
            gap: 15px;
        }

        .nav-button {
            padding: 12px 25px;
            background: var(--secondary-color);
            color: var(--background-color);
            border: none;
            border-radius: 4px;
            font-size: 15px;
            cursor: pointer;
            transition: all 0.3s ease;
            font-weight: bold;
            text-transform: uppercase;
            letter-spacing: 1px;
            box-shadow: 0 0 15px rgba(255, 0, 255, 0.3);
        }

        .username {
            color: var(--primary-color);
            font-size: 16px;
            font-weight: 500;
            text-shadow: 0 0 10px rgba(0, 255, 157, 0.3);
            text-align: right;
        }

        h1 {
            color: var(--primary-color);
            text-align: center;
            margin: 10px 0;
            font-size: 2em;
            text-shadow: 0 0 10px rgba(0, 255, 157, 0.3);
            letter-spacing: 2px;
        }

        h1 .brand {
            color: var(--secondary-color);
            font-size: 0.8em;
            display: block;
            margin-bottom: 5px;
            text-shadow: 0 0 10px rgba(255, 0, 255, 0.3);
        }

        .upload-container, .input-container, .button-container, .result-container {
            max-width: 95%;
            margin: 15px auto;
            padding: 20px;
            background: var(--card-background);
            border-radius: 8px;
            box-shadow: 0 0 20px rgba(0, 255, 157, 0.1);
            border: 1px solid var(--border-color);
            position: relative;
            overflow: hidden;
        }

        .upload-container::before, .input-container::before, .button-container::before, .result-container::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 2px;
            background: linear-gradient(90deg, var(--primary-color), var(--secondary-color));
        }

        .file-upload {
            display: flex;
            flex-direction: column;
            align-items: center;
            gap: 15px;
        }

        .file-upload input[type="file"] {
            display: none;
        }

        .file-upload label {
            padding: 12px 25px;
            background: var(--primary-color);
            color: var(--background-color);
            border-radius: 4px;
            cursor: pointer;
            transition: all 0.3s ease;
            font-weight: bold;
            text-transform: uppercase;
            letter-spacing: 1px;
            box-shadow: 0 0 15px rgba(0, 255, 157, 0.3);
        }

        .file-upload label:hover {
            background: var(--hover-color);
            transform: translateY(-2px);
            box-shadow: 0 0 20px rgba(0, 255, 157, 0.5);
        }

        .file-name {
            color: var(--text-color);
            font-size: 14px;
            opacity: 0.8;
        }

        .error-message {
            color: var(--error-color);
            font-size: 14px;
            margin-top: 5px;
            display: none;
            text-shadow: 0 0 10px rgba(255, 51, 102, 0.3);
        }

        .input-container textarea {
            width: 100%;
            min-height: 120px;
            padding: 12px;
            background: var(--background-color);
            border: 1px solid var(--border-color);
            border-radius: 4px;
            color: var(--text-color);
            font-family: 'Microsoft YaHei', sans-serif;
            font-size: 14px;
            resize: vertical;
            transition: all 0.3s ease;
        }

        .input-container textarea:focus {
            outline: none;
            border-color: var(--primary-color);
            box-shadow: 0 0 15px rgba(0, 255, 157, 0.2);
        }

        .input-container textarea::placeholder {
            color: rgba(255, 255, 255, 0.5);
        }

        .generate-button, .save-button {
            padding: 12px 30px;
            background: var(--primary-color);
            color: var(--background-color);
            border: none;
            border-radius: 4px;
            font-size: 15px;
            cursor: pointer;
            transition: all 0.3s ease;
            font-weight: bold;
            text-transform: uppercase;
            letter-spacing: 1px;
            box-shadow: 0 0 15px rgba(0, 255, 157, 0.3);
        }

        .generate-button:hover, .save-button:hover {
            background: var(--hover-color);
            transform: translateY(-2px);
            box-shadow: 0 0 20px rgba(0, 255, 157, 0.5);
        }

        .generate-button:disabled {
            background: var(--border-color);
            cursor: not-allowed;
            transform: none;
            box-shadow: none;
        }

        .result-content {
            color: var(--text-color);
            line-height: 1.6;
            font-size: 14px;
        }

        .result-content h1,
        .result-content h2,
        .result-content h3,
        .result-content h4,
        .result-content h5,
        .result-content h6 {
            color: var(--primary-color);
            margin-top: 20px;
            margin-bottom: 15px;
            font-weight: 600;
            line-height: 1.4;
            text-shadow: 0 0 10px rgba(0, 255, 157, 0.2);
        }

        .result-content h1 {
            font-size: 1.8em;
            border-bottom: 2px solid var(--primary-color);
            padding-bottom: 0.3em;
        }

        .result-content h2 {
            font-size: 1.4em;
            border-bottom: 1px solid var(--border-color);
            padding-bottom: 0.3em;
        }

        .result-content p {
            margin: 0.8em 0;
        }

        .result-content ul,
        .result-content ol {
            padding-left: 1.5em;
            margin: 0.8em 0;
        }

        .result-content li {
            margin: 0.4em 0;
        }

        .result-content code {
            padding: 0.2em 0.4em;
            background: var(--background-color);
            border: 1px solid var(--border-color);
            border-radius: 3px;
            font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, Courier, monospace;
            color: var(--primary-color);
        }

        .result-content pre {
            padding: 0.8em;
            background: var(--background-color);
            border: 1px solid var(--border-color);
            border-radius: 4px;
            overflow-x: auto;
        }

        .result-content pre code {
            padding: 0;
            border: none;
            background: none;
        }

        .result-content blockquote {
            padding: 0.8em;
            border-left: 4px solid var(--primary-color);
            background: rgba(0, 255, 157, 0.1);
            margin: 0.8em 0;
        }

        .result-content table {
            width: 100%;
            border-collapse: collapse;
            margin: 0.8em 0;
            background: var(--background-color);
        }

        .result-content table th,
        .result-content table td {
            padding: 0.6em;
            border: 1px solid var(--border-color);
        }

        .result-content table th {
            background: rgba(0, 255, 157, 0.1);
            color: var(--primary-color);
        }

        .result-content table tr:nth-child(even) {
            background: rgba(0, 255, 157, 0.05);
        }

        .loading-container {
            text-align: center;
            margin: 20px 0;
            display: none;
        }

        .loading-spinner {
            width: 40px;
            height: 40px;
            border: 4px solid var(--border-color);
            border-top: 4px solid var(--primary-color);
            border-radius: 50%;
            animation: spin 1s linear infinite;
            margin: 0 auto;
            box-shadow: 0 0 20px rgba(0, 255, 157, 0.2);
        }

        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }

        .hint-message {
            color: var(--text-color);
            font-size: 14px;
            text-align: center;
            margin-top: 10px;
            opacity: 0.8;
        }

        @media (min-width: 1200px) {
            .upload-container, .input-container, .button-container, .result-container {
                max-width: 1100px;
            }
        }

        @media (min-width: 992px) and (max-width: 1199px) {
            .upload-container, .input-container, .button-container, .result-container {
                max-width: 900px;
            }
        }

        @media (min-width: 768px) and (max-width: 991px) {
            .upload-container, .input-container, .button-container, .result-container {
                max-width: 95%;
            }
        }

        @media (max-width: 768px) {
            .upload-container,
            .input-container,
            .button-container,
            .result-container {
                margin: 10px;
                padding: 15px;
                max-width: calc(100% - 20px);
            }

            .generate-button,
            .save-button {
                width: 100%;
                padding: 10px 20px;
            }

            .file-upload label {
                width: 100%;
                text-align: center;
            }

            .header {
                grid-template-columns: 1fr;
                gap: 10px;
                text-align: center;
            }

            .header-buttons {
                flex-direction: column;
                gap: 10px;
                order: 2;
            }

            .username {
                text-align: center;
                order: 3;
            }

            .nav-button {
                width: 100%;
            }

            h1 {
                font-size: 1.6em;
            }
            h1 .brand {
                font-size: 0.7em;
            }
        }

        @keyframes glow {
            0% { box-shadow: 0 0 5px var(--primary-color); }
            50% { box-shadow: 0 0 20px var(--primary-color); }
            100% { box-shadow: 0 0 5px var(--primary-color); }
        }

        .upload-container:hover,
        .input-container:hover,
        .button-container:hover,
        .result-container:hover {
            animation: glow 2s infinite;
        }
    </style>
</head>
<body>
    <div class="header">
        <button class="nav-button" onclick="window.location.href='{{ front_end_domain_name }}/navigation_page'">导航</button>
        <h1>
            <span class="brand">EduBot</span>
            PPTX 教案生成
        </h1>
        <div class="username" id="usernameDisplay"></div>
    </div>

    <div class="upload-container">
        <div class="file-upload">
            <input type="file" id="pptxFile" accept=".pptx" onchange="handleFileSelect(event)">
            <label for="pptxFile">选择 PPTX 文件</label>
            <div class="file-name" id="fileName"></div>
            <div class="error-message" id="errorMessage"></div>
            <div class="hint-message">提示：PPTX 文件为可选项，不上传也可以生成教案</div>
        </div>
    </div>

    <div class="input-container">
        <textarea id="userPrompt" placeholder="请输入您的教案生成要求，例如：&#10;1. 教案要包含教学目标、教学重难点&#10;2. 教案要包含教学步骤和教学方法&#10;3. 教案要包含课堂练习和作业布置"></textarea>
    </div>

    <div class="button-container">
        <button class="generate-button" id="generateButton" onclick="handleGenerate()">开始生成</button>
    </div>

    <div class="loading-container" id="loadingContainer">
        <div class="loading-spinner"></div>
    </div>

    <div class="result-container" id="resultContainer">
        <div class="result-content" id="resultContent"></div>
        <button class="save-button" id="saveButton" style="display: none;">保存教学方案</button>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', async function() {
            const token = localStorage.getItem('token');
            if (!token) {
                window.location.href = '{{ front_end_domain_name }}/login_register_page';
                return;
            }

            const username = await window.checkTokenApi('{{ backend_domain_name }}', token);
            if (!username) {
                window.location.href = '{{ front_end_domain_name }}/login_register_page';
                return;
            }

            document.getElementById('usernameDisplay').textContent = `欢迎，${username}`;
        });

        function handleFileSelect(event) {
            const file = event.target.files[0];
            const fileNameDisplay = document.getElementById('fileName');
            const errorMessage = document.getElementById('errorMessage');
            
            if (!file) {
                fileNameDisplay.textContent = '';
                errorMessage.style.display = 'none';
                return;
            }

            if (file.type !== 'application/vnd.openxmlformats-officedocument.presentationml.presentation') {
                errorMessage.textContent = '请选择 PPTX 格式的文件';
                errorMessage.style.display = 'block';
                fileNameDisplay.textContent = '';
                event.target.value = '';
                return;
            }

            errorMessage.style.display = 'none';
            fileNameDisplay.textContent = `已选择: ${file.name}`;
        }

        function handleGenerate() {
            const file = document.getElementById('pptxFile').files[0];
            const userPrompt = document.getElementById('userPrompt').value.trim();
            const generateButton = document.getElementById('generateButton');
            const loadingContainer = document.getElementById('loadingContainer');
            const resultContainer = document.getElementById('resultContainer');
            const resultContent = document.getElementById('resultContent');
            const saveButton = document.getElementById('saveButton');

            if (!userPrompt && !file) {
                alert('请上传 PPTX 文件或填写教案生成要求');
                return;
            }

            generateButton.disabled = true;
            loadingContainer.style.display = 'block';
            resultContainer.style.display = 'none';
            saveButton.style.display = 'none';

            let generatedContent = '';

            const processGeneration = (pptxText) => {
                const token = localStorage.getItem('token');
                
                if (!userPrompt && (!pptxText || pptxText.trim() === '')) {
                    alert('请上传 PPTX 文件或填写教案生成要求');
                    generateButton.disabled = false;
                    loadingContainer.style.display = 'none';
                    return;
                }
                
                const finalPptxText = pptxText || ' ';
                const finalUserPrompt = userPrompt || ' ';
                
                window.getLessonPlansApi('{{ backend_domain_name }}', token, finalPptxText, finalUserPrompt)
                    .then(response => {
                        if (response.TF) {
                            generatedContent = response.ai_output;
                            resultContent.innerHTML = marked.parse(response.ai_output);
                            resultContainer.style.display = 'block';
                            saveButton.style.display = 'block';
                        } else {
                            alert(response.message || '生成失败');
                        }
                    })
                    .catch(error => {
                        alert('生成过程中出现错误');
                        console.error(error);
                    })
                    .finally(() => {
                        generateButton.disabled = false;
                        loadingContainer.style.display = 'none';
                    });
            };

            if (file) {
                window.extractPptxText(file)
                    .then(pptxText => {
                        const textContent = pptxText.filter(Boolean).join('\n');
                        processGeneration(textContent);
                    })
                    .catch(error => {
                        alert('PPTX 文件处理失败');
                        console.error(error);
                        generateButton.disabled = false;
                        loadingContainer.style.display = 'none';
                    });
            } else {
                processGeneration(' ');
            }

            saveButton.onclick = function() {
                saveLessonPlan(generatedContent, file);
            };
        }

        function saveLessonPlan(content, pptxFile) {
            const htmlContent = `
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>教学方案</title>
    <style>
        body {
            font-family: 'Microsoft YaHei', sans-serif;
            line-height: 1.6;
            padding: 20px;
            max-width: 800px;
            margin: 0 auto;
        }
        h1, h2, h3, h4, h5, h6 {
            color: #333;
            margin-top: 1.5em;
            margin-bottom: 0.5em;
        }
        p {
            margin: 0.8em 0;
        }
        ul, ol {
            padding-left: 2em;
            margin: 0.8em 0;
        }
        li {
            margin: 0.4em 0;
        }
        code {
            background: #f5f5f5;
            padding: 0.2em 0.4em;
            border-radius: 3px;
        }
        pre {
            background: #f5f5f5;
            padding: 1em;
            border-radius: 5px;
            overflow-x: auto;
        }
        blockquote {
            border-left: 4px solid #ddd;
            padding-left: 1em;
            color: #666;
            margin: 1em 0;
        }
        table {
            border-collapse: collapse;
            width: 100%;
            margin: 1em 0;
        }
        th, td {
            border: 1px solid #ddd;
            padding: 0.5em;
            text-align: left;
        }
        th {
            background: #f5f5f5;
        }
        @media print {
            body {
                padding: 0;
            }
            pre {
                white-space: pre-wrap;
            }
        }
    </style>
</head>
<body>
${marked.parse(content)}
</body>
</html>`;
            
            const blob = new Blob([htmlContent], { type: 'text/html;charset=utf-8' });
            
            const link = document.createElement('a');
            
            const now = new Date();
            const dateStr = `${now.getFullYear()}${(now.getMonth()+1).toString().padStart(2, '0')}${now.getDate().toString().padStart(2, '0')}`;
            const timeStr = `${now.getHours().toString().padStart(2, '0')}${now.getMinutes().toString().padStart(2, '0')}`;
            
            let fileName = '';
            
            if (pptxFile) {
                const pptxName = pptxFile.name.replace(/\.pptx$/i, '');
                fileName = `${pptxName}_教学方案_${dateStr}_${timeStr}.html`;
            } else {
                fileName = `教学方案_${dateStr}_${timeStr}.html`;
            }
            
            link.href = URL.createObjectURL(blob);
            link.download = fileName;
            
            document.body.appendChild(link);
            link.click();
            
            document.body.removeChild(link);
            URL.revokeObjectURL(link.href);
        }
    </script>
</body>
</html>